< previous page page_410 next page >

Page 410
z0430-01.gif
Figure 12.24
Using a quadtree to decide which objects to draw.
clicked is in the northwest quadrant, so you examine the node's northwest child. When you examine that node, you find that the selected point is in its the northeast quadrant. That node is not subdivided further. The object clicked by the user must be in that node, so you need to search only the objects in that node.
In this example, you would find that object C contained the point the user selected. Instead of examining every object in the picture, you are able to use the quadtree to narrow the search down to the objects contained in a single quadtree node. In this case, that node contained only the single object C, and that was the one you wanted to find.
Quadtrees in Visual Basic
You must handle a couple of details when implementing quadtrees in Visual Basic. First, there is a tradeoff between the number of quadtree nodes you use and the number of objects you can eliminate when displaying or searching the picture. If you use many quadtree nodes, each will contain only a few objects, so searches will be relatively fast. On the other hand, you will have used a lot of memory to create the nodes. If you use too many nodes, the extra memory you use will slow the drawing noticeably.
On the other hand, if you use too few quadtree nodes, you will not eliminate many of the nodes when you are drawing or searching, and the quadtree will be of little help. In that case, you will have made your job more complicated for little gain.
You must find a balance when deciding how many objects to allow in each quadtree node. As a rule, allowing 100 or so objects per node usually provides reasonable performance without too much memory overhead. You will need to experiment a little with your application to see what numbers are best.
A second detail to consider is that most graphical objects have a nonzero width and height. This means a single object can intersect more than one quadtree node. Figure 12.25 shows several objects that intersect more than one quadtree node.

 
< previous page page_410 next page >